//
//  BlockActionSheet.m
//  WeiboFun
//
//  Created by luo danal on 8/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BlockActionSheet.h"

@implementation BlockActionSheet

- (void)dealloc{
    Block_release(_clickBlock);
    Block_release(_dismissBlock);
    [super dealloc];
}

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...{
    
    if (otherButtonTitles != nil) { //Generally in this case
        
        va_list apList;
        va_start(apList, otherButtonTitles);
        NSString *ttl = nil;
        self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles, nil];
        if (self) {
            while ((ttl = va_arg(apList, NSString*))) {
                [self addButtonWithTitle:ttl];
            } 
            if (cancelButtonTitle != nil) {
                [self addButtonWithTitle:cancelButtonTitle];
                [self setCancelButtonIndex:self.numberOfButtons - 1];
            }
        }
        va_end(apList);
    } else {    //No other buttons spcified   
        
        self = [super initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil, nil];
    }
    return self;
}

- (id)initWithTitle:(NSString *)title
    buttonTitleList:(NSArray *)otherButtonTitles
  cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle{
    NSString *otherTitle = [otherButtonTitles count] > 0 ? [otherButtonTitles objectAtIndex:0] : nil;
    self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherTitle, nil];
    if (self) {
        if ([otherButtonTitles count] > 0){
            for (int i = 1; i < [otherButtonTitles count]; i++){
                [self addButtonWithTitle:[otherButtonTitles objectAtIndex:i]];
            }
        }
        if (cancelButtonTitle != nil) {
            [self addButtonWithTitle:cancelButtonTitle];
            [self setCancelButtonIndex:self.numberOfButtons - 1];
        }
    }
    return self;
}

- (void)setClickBlock:(void (^)(int,BlockActionSheet *))block{
    Block_release(_clickBlock);
    _clickBlock = Block_copy(block);
}

- (void)setDismissBlock:(void (^)(int,BlockActionSheet *))block{
    Block_release(_dismissBlock);
    _dismissBlock = Block_copy(block);
}

#pragma mark - delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (_clickBlock != NULL) {
        _clickBlock(buttonIndex,self);
    }    
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (_dismissBlock != NULL) {
        _dismissBlock(buttonIndex,self);
    }
}

@end
